import { createStore } from 'vuex'
import weaponsData from '@/assets/weapons.json'
import auth from './modules/auth'

export default createStore({
  state() {
    return {
      weapons: weaponsData.weapons,
      searchQuery: '',
      showOnlyOwned: false,
      recommendedWeapons: JSON.parse(localStorage.getItem('recommendedWeapons')) || [],
      wishlist: JSON.parse(localStorage.getItem('wishlist')) || []
    }
  },
  mutations: {
    toggleWeaponOwnership(state, weaponId) {
      const weapon = state.weapons.find(w => w.id === weaponId)
      if (weapon) {
        weapon.owned = !weapon.owned
        // 如果装备被标记为已拥有，从愿望清单中移除
        if (weapon.owned) {
          const wishlistIndex = state.wishlist.indexOf(weaponId)
          if (wishlistIndex !== -1) {
            state.wishlist.splice(wishlistIndex, 1)
            localStorage.setItem('wishlist', JSON.stringify(state.wishlist))
          }
        }
      }
    },
    setSearchQuery(state, query) {
      state.searchQuery = query
    },
    toggleShowOnlyOwned(state) {
      state.showOnlyOwned = !state.showOnlyOwned
    },
    toggleRecommended(state, weaponId) {
      const index = state.recommendedWeapons.indexOf(weaponId)
      if (index === -1) {
        state.recommendedWeapons.push(weaponId)
      } else {
        state.recommendedWeapons.splice(index, 1)
      }
      // 保存到本地存储
      localStorage.setItem('recommendedWeapons', JSON.stringify(state.recommendedWeapons))
    },
    toggleWishlist(state, weaponId) {
      const index = state.wishlist.indexOf(weaponId)
      if (index === -1) {
        state.wishlist.push(weaponId)
      } else {
        state.wishlist.splice(index, 1)
      }
      localStorage.setItem('wishlist', JSON.stringify(state.wishlist))
    }
  },
  actions: {
    toggleRecommendedWeapon({ commit }, weaponId) {
      commit('toggleRecommended', weaponId)
    },
    toggleWishlistItem({ commit, state }, weaponId) {
      const weapon = state.weapons.find(w => w.id === weaponId)
      if (weapon && !weapon.owned) {
        commit('toggleWishlist', weaponId)
      }
    }
  },
  getters: {
    filteredWeapons: (state) => (type = '全部') => {
      let weapons = state.weapons
      
      if (type && type !== '全部') {
        weapons = weapons.filter(w => w.type === type)
      }
      
      if (state.showOnlyOwned) {
        weapons = weapons.filter(w => w.owned)
      }
      
      return weapons
    },
    searchResults: (state) => {
      if (!state.searchQuery) return []
      
      return state.weapons.filter(weapon => 
        weapon.name.toLowerCase().includes(state.searchQuery.toLowerCase()) ||
        weapon.type.toLowerCase().includes(state.searchQuery.toLowerCase())
      )
    },
    recommendedWeapons: (state) => {
      return state.weapons.filter(weapon => state.recommendedWeapons.includes(weapon.id))
    },
    wishlistWeapons: (state) => {
      return state.weapons.filter(weapon => state.wishlist.includes(weapon.id))
    },
    isInWishlist: (state) => (weaponId) => {
      return state.wishlist.includes(weaponId)
    }
  },
  modules: {
    auth
  }
})
